package utils;

import bean.Clothes;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.Xpp3DomDriver;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ProductsXMLUtils {

    //解析XML文件放入产品列表
    public static List<Clothes> parseProductFormXML() {
        List<Clothes> products = new ArrayList<>();
        XStream xStream = new XStream(new Xpp3DomDriver());
        xStream.alias("list", products.getClass());
        xStream.alias("clothes", Clothes.class);
        xStream.useAttributeFor(Clothes.class, "id");
        //读取XML文件
        try {
            BufferedInputStream inputStream = new BufferedInputStream(
                    new FileInputStream("products.xml"));
            products = ((List<Clothes>) xStream.fromXML(inputStream));
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return products;
    }

    //将产品列表写入XML中
    public static void writeProductToXML(List<Clothes> products) {
        XStream xStream = new XStream(new Xpp3DomDriver());
        xStream.alias("list", products.getClass());
        xStream.alias("clothes", Clothes.class);
        xStream.useAttributeFor(Clothes.class, "id");
        try {
            BufferedOutputStream outputStream = new BufferedOutputStream(
                    new FileOutputStream("products.xml"));
            outputStream.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>".getBytes());
            xStream.toXML(products, outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
